<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="听课日期" prop="listenDate">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="课程" prop="courseId">
       <el-select v-model="queryParams.courseId" clearable filterable placeholder="请输入课程名选择">
         <el-option
           v-for="item in courseList"
           :key="item.id"
           :label="item.name + '('+item.code+')'"
           :value="item.id"
           size="small">
         </el-option>
       </el-select>
      </el-form-item>
      <el-form-item label="教师" prop="teacherId">
       <el-select v-model="queryParams.teacherId" clearable filterable placeholder="请输入姓名选择">
         <el-option
           v-for="item in userOptions"
           :key="item.userId"
           :label="item.nickName + '('+item.userName+')'"
           :value="item.userId"
           size="small">
         </el-option>
       </el-select>
      </el-form-item>
      <el-form-item label="审核结果" prop="checkStatus">
        <el-select v-model="queryParams.checkStatus" placeholder="请选择审核结果" clearable size="small">
          <el-option
            v-for="dict in checkStatusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['listen:apply:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['listen:apply:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['listen:apply:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="applyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="听课日期" align="center" prop="listenDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.listenDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="课程" align="center" prop="coursenName" />
      <el-table-column label="授课教师" align="center" prop="teacherName" />
      <el-table-column label="节次" align="center" prop="section" :formatter="sectionFormat" />
      <el-table-column label="审核结果" align="center" prop="checkStatus" :formatter="checkStatusFormat" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['listen:apply:edit']"
          >修改</el-button>
          <el-button v-if="scope.row.checkStatus == '0'"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['listen:apply:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改听课申请对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="听课日期" prop="listenDate">
          <el-date-picker clearable size="small" :disabled="form.checkStatus == '1'"
            v-model="form.listenDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择听课日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="课程" prop="courseId">
          <el-select v-model="form.courseId" clearable :disabled="form.checkStatus == '1'"
          filterable  placeholder="请输入课程名选择"
          >
            <el-option
              v-for="item in courseList"
              :key="item.id"
              :label="item.name + '('+item.code+')'"
              :value="item.id"
              size="small">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="教师" prop="teacherId">
          <el-select v-model="form.teacherId" :disabled="form.checkStatus == '1'"
          clearable filterable placeholder="请输入姓名选择">
            <el-option
              v-for="item in userOptions"
              :key="item.userId"
              :label="item.nickName + '('+item.userName+')'"
              :value="item.userId"
              size="small">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="节次" prop="section">
          <el-select v-model="form.section" :disabled="form.checkStatus == '1'" placeholder="请选择节次">
            <el-option
              v-for="dict in sectionOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="申请原因" prop="reason">
          <el-input v-model="form.reason" placeholder="请输入申请原因" :disabled="form.checkStatus == '1'"/>
        </el-form-item>
        <el-form-item label="评语" prop="remark" v-if="form.checkStatus == '1'">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMyApply, getApply, delApply, addApply, updateApply, exportApply } from "@/api/listen/apply";
import { selectCourse} from "@/api/teach/course";
import { selectUser } from "@/api/system/user";

export default {
  name: "MyApply",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 听课申请表格数据
      applyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 节次字典
      sectionOptions: [],
      // 课程数据
      courseList: [],
      // 用户选项
      userOptions: [],
      // 审核结果字典
      checkStatusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        listenDate: null,
        courseId: null,
        teacherId: null,
        section: null,
        checkUserId: null,
        checkStatus: null,
        checkTime: null,
      },
      dateRange: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        listenDate: [
          { required: true, message: "听课日期不能为空", trigger: "blur" }
        ],
        courseId: [
          { required: true, message: "课程不能为空", trigger: "blur" }
        ],
        teacherId: [
          { required: true, message: "教师不能为空", trigger: "blur" }
        ],
        section: [
          { required: true, message: "节次不能为空", trigger: "change" }
        ],
        reason: [
          { required: true, message: "申请原因不能为空", trigger: "blur" }
        ],
        createBy: [
          { required: true, message: "创建人不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("teach_section").then(response => {
      this.sectionOptions = response.data;
    });
    this.getDicts("check_status").then(response => {
      this.checkStatusOptions = response.data;
    });
    selectCourse().then(response => {
      this.courseList = response.rows;
    });
    selectUser({status:'0'}).then(response => {
      this.userOptions = response.rows;
    });
  },
  methods: {
    /** 查询听课申请列表 */
    getList() {
      this.loading = true;
      listMyApply(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.applyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 节次字典翻译
    sectionFormat(row, column) {
      return this.selectDictLabel(this.sectionOptions, row.section);
    },
    // 审核结果字典翻译
    checkStatusFormat(row, column) {
      return this.selectDictLabel(this.checkStatusOptions, row.checkStatus);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        listenDate: null,
        courseId: null,
        teacherId: null,
        section: null,
        reason: null,
        remark: null,
        checkUserId: null,
        checkStatus: "0",
        checkTime: null,
        checkReason: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加听课申请";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getApply(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改听课申请";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateApply(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addApply(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除听课申请编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delApply(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有听课申请数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportApply(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
